package com.zjitc.model;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/24 15:55
 * @description:
 */
public class History {
  private static final int MAX_SIZE=8;
  private Map<String,Product> productMap=new LinkedHashMap<>();
  Queue<String> queue=new LinkedList<>();

  public static int getMaxSize() {
    return MAX_SIZE;
  }

  public Map<String, Product> getProductMap() {
    return productMap;
  }

  public void setProductMap(Map<String, Product> productMap) {
    this.productMap = productMap;
  }

  public Queue<String> getQueue() {
    return queue;
  }

  public void setQueue(Queue<String> queue) {
    this.queue = queue;
  }

  public History(Map<String, Product> productMap, Queue<String> queue) {

    this.productMap = productMap;
    this.queue = queue;
  }

  public History() {

  }

  public void add(Product product){
    String pid=product.getPid();
    if(productMap.containsKey(pid)){
      return;
    }
    productMap.put(pid,product);
    queue.add(pid);
    while (queue.size()>=MAX_SIZE){
      String q=queue.poll();
      productMap.remove(q);
    }
  }
}
